package com.antdesignbackend.backend.activiti.reviewer.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.activiti.reviewer.entity.Reviewer;
import com.antdesignbackend.backend.activiti.reviewer.entity.entityVo.ReviewerVo;
import com.antdesignbackend.backend.activiti.reviewer.service.ReviewerService;

/**
 * 配置审批人
 *
 * @author 不二
 * @email reviewer
 * @date 2020-08-12 11:05:49
 */

@Controller
@RequestMapping("/reviewer")
@Slf4j
public class ReviewerController extends BaseController{

	private static final String FUN_NAME = "配置审批人";//功能名称

	private Integer processListId;

	@Autowired
	private ReviewerService reviewerService;

	@GetMapping("/index")
	@AuthToken
	public String index(Integer processListId) {
		this.processListId = processListId;
		return "activiti/reviewer/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody ReviewerVo reviewerVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<ReviewerVo> list = new ArrayList<>();
		QueryWrapper<ReviewerVo> wrapper = new QueryWrapper<>();
		wrapper.eq("processListId",this.processListId);
		wrapper.orderByAsc("orderId");
		int total = reviewerService.count(wrapper);
		if (total > 0){
			list = reviewerService.search(wrapper,new Page<ReviewerVo>(reviewerVo.getPageIndex(),reviewerVo.getPageSize()));
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	Reviewer getById(@PathVariable(name = "id") String id) {
		Reviewer obj = reviewerService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Reviewer reviewer){
		ReturnVO result = new ReturnVO();
		reviewer.setProcessListId(this.processListId);
		reviewer.setCreateBy(getLoginUserName());
		reviewer.setCreateTime(new Date());
		if(reviewerService.save(reviewer)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Reviewer reviewer){
		ReturnVO result = new ReturnVO();
		reviewer.setUpdateBy(getLoginUserName());
		reviewer.setUpdateTime(new Date());
		if(reviewerService.updateById(reviewer)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (reviewerService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

}
